package org.example.synchronizedSty;
/*
     编写一个多线程程序，实现这样一个目标：
1.存在一个对象，该对象有一个int类型的成员变量counter,该成员变量的初始值为0。
2.创建两个线程，其中一个线程对该对象的成员变量counter增1,另一个线程对该对象的成员变量减1。
3.输出该对象成员变量counter每次变化后的值。
4.最终输出的结果应为：1010101010101。。。。。

* */
public class ThreadWaitNotify {
    int counter = 0;

    public static void main(String[] args) throws InterruptedException {
        ThreadWaitNotify twn = new ThreadWaitNotify();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (twn) {
                        while(twn.counter==0) {
                            if (twn.counter == 0) {
                                twn.counter++;
                                System.out.println(twn.counter);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                twn.notify();

                            } else {
                                try {
                                    twn.wait();
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    synchronized (twn) {
                        while(twn.counter==0) {
                            if (twn.counter == 0) {
                                twn.counter++;
                                System.out.println(twn.counter);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                twn.notify();

                            } else {
                                try {
                                    twn.wait();
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (twn) {
                        while (twn.counter == 1) {
                            if (twn.counter == 1) {
                                twn.counter--;
                                System.out.println(twn.counter);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                twn.notify();

                            } else {
                                try {
                                    twn.wait();
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (twn) {
                        while (twn.counter == 1) {
                            if (twn.counter == 1) {
                                twn.counter--;
                                System.out.println(twn.counter);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                twn.notify();

                            } else {
                                try {
                                    twn.wait();
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                }
            }
        }).start();
    }
}
